package com.goldcn.config;

import com.mongodb.Mongo;
import com.mongodb.MongoClientOptions;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;

/**
 * Created by kylin.woo on 3/4/2016.
 */
//@Configuration
//@EnableConfigurationProperties(MongoProperties.class)
//@ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoDBConfiguration {

    @Autowired
    private MongoProperties properties;
    @Autowired(required = false)
    private MongoClientOptions options;
    private Mongo mongo;
    @PreDestroy
    public void close() {
        if (this.mongo != null) {
            this.mongo.close();
        }
    }
    @Bean
    public Mongo mongo() throws Exception {
        this.mongo = this.properties.createMongoClient(this.options);
        return this.mongo;
    }

}
